#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>

#include "../notifyADT/notifyADT.h"
#include "../include/client.h"
#include "../../common/include/common.h"

#include "../include/pipesclient.h"
#include "../../common/include/commonPI.h"

#include "../include/MQclient.h"
#include "../../common/include/commonMQ.h"

#include "../include/TCPclient.h"
#include "../../common/include/commonTCP.h"

#include "../include/shm_client.h"
#include "../../common/include/commonSHM.h"

#include "../../common/include/filesystem.h"
#include "../../common/include/general.h"
#include "../../common/include/menu.h"
#include "../../common/include/split.h"

/*Variable para saber que estamos loggeados */
int logged = 0;

/* Fds y path de los fifos */
watchD w;

/* Dir donde se va a escribir */
char *syncPath = ".";

/*Variables para split */
char splitVal;
pthread_t threads[SPLITS_QUANTITY];
int sons[SPLITS_QUANTITY];

/* Flag para el ciclo de escucha del server */
int doLS = 1;

/* Ultimo path recibido */
char lastComm[FILE_LEN] = {0};

int
main(int argc, char ** argv)
{
    appInit params;

    getParameters(argc, argv, &params);

    if (!validateParameters(&params))
    {
        usage(argv[0]);
        return 0;
    }

    /* Lógica de inicialización del programa con params */
    logged = 0;
    splitVal = params.split;

    switch(params.ipc)
    {
        case 'p':
            mainPI();
            break;

        case 'q':
             mainMQ();
            break;

        case 'm':
            mainSM();
            break;

        case 's':
             mainTCP();
            break;

        default:
            fatal(  "ERROR: Wrong IPC.\n"
                    "SOLUTION: Check program help.\n");
            break;
    }

    freeParameters(&params);
    return 0;
}

void
getDirType(char *params, char *dir, int *type)
{
    char c;
    int i = 0;

    if (params == NULL || dir == NULL || type == NULL)
        return;

    do{
        c = params[i];
        dir[i++] = c;
    }while(c != ',');

    dir[i-1] = 0;
    sscanf(params + i, "%d", type);

    return;
}
